package com.hwn.datax.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.hwn.datax.domain.datax.RangeOption;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import lombok.Getter;
import org.springframework.stereotype.Controller;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.function.Consumer;

/**
 * @author wnhuang
 * @date 2024/6/7 15:50
 */
@Controller
public class RunRangeOptionController {
    ToggleGroup toggleGroup = new ToggleGroup();
    @FXML
    public RadioButton rangeRbtn;
    @FXML
    public RadioButton dateRbtn;
    @FXML
    private HBox datePane, rangePane;
    @FXML
    private DatePicker startDatePicker, endDatePicker;
    @FXML
    private TextField startValueField, endValueField, stepField;
    @FXML
    private TextArea replaceStrArea;

    // 添加RangeOption属性
    @Getter
    private RangeOption rangeOption;

    public void setRangeOption(RangeOption rangeOption, Consumer<RangeOption> callback) {
        this.callback = callback;
        this.rangeOption = new RangeOption();
        this.rangeOption.setType("date");
        BeanUtil.copyProperties( rangeOption , this.rangeOption);
        updateUIFromModel();
    }

    private Consumer<RangeOption> callback;


    // 确认按钮事件处理
    @FXML
    private void handleConfirm() {
        updateModelFromUI(); // 更新RangeOption对象
        if (callback != null) {
            callback.accept(rangeOption);
        }
        closeStage();
    }

    // 取消按钮事件处理
    @FXML
    private void handleCancel() {
        closeStage();
    }

    // 关闭当前Stage的方法
    private void closeStage() {
        Stage stage = (Stage) replaceStrArea.getScene().getWindow();
        stage.close();
    }

    @FXML
    private void initialize() {

        dateRbtn.setToggleGroup(toggleGroup);
        rangeRbtn.setToggleGroup(toggleGroup);

        toggleGroup.selectedToggleProperty().addListener((observable, oldValue, newValue) -> onTypeChanged());
        // 初始化时，可以根据需要加载默认的rangeOption数据
    }

    @FXML
    private void onTypeChanged() {
        RadioButton selected = (RadioButton) toggleGroup.getSelectedToggle();
        if (selected != null) {
            boolean isDate = selected.getText().equals("Date");
            datePane.setVisible(isDate);
            rangePane.setVisible(!isDate);
        }
    }

    // 从UI更新RangeOption对象
    public void updateModelFromUI() {
        rangeOption.setType(((RadioButton) toggleGroup.getSelectedToggle()).getText().toLowerCase());
        if (rangeOption.getType().equals("date")) {
            rangeOption.setStartDate(startDatePicker.getValue());
            rangeOption.setEndDate(endDatePicker.getValue());
        }
        if (rangeOption.getType().equals("range")) {
            rangeOption.setStartValue(Convert.toLong(startValueField.getText(), null));
            rangeOption.setEndValue(Convert.toLong(endValueField.getText(), null));
        }
        rangeOption.setStep(Convert.toLong(stepField.getText(), null));
        rangeOption.setReplaceStr(replaceStrArea.getText());

    }

    // 根据RangeOption对象更新UI
    public void updateUIFromModel() {
        String type = rangeOption.getType();
        if ("date".equals(type)) {
            dateRbtn.setSelected(true);
//          toggleGroup.selectToggle(toggleGroup.getToggles().get(0)); // Assuming the first toggle is "Date"
            startDatePicker.setValue(rangeOption.getStartDate());
            endDatePicker.setValue(rangeOption.getEndDate());
        } else if ("range".equals(type)) {
            rangeRbtn.setSelected(true);
//            toggleGroup.selectToggle(toggleGroup.getToggles().get(1)); // Assuming the second toggle is "Range"
            startValueField.setText(  Convert.toStr(rangeOption.getStartValue(), "")   );
            endValueField.setText(  Convert.toStr(rangeOption.getEndValue(), "") );
        }
        stepField.setText(Convert.toStr(rangeOption.getStep(), ""));
        replaceStrArea.setText(rangeOption.getReplaceStr());
        onTypeChanged(); // To update the visibility of datePane and rangePane
    }
}
